package com.mtnz.njk.Service;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.mtnz.njk.Mapper.ExpressManagerMapper;
import com.mtnz.njk.Mapper.ExpressMapper;
import com.mtnz.njk.Util.ExcelUtils;
import com.mtnz.njk.entity.ExpressEntity;
import com.mtnz.njk.result.PageInfo;
import com.mtnz.njk.result.ResponseMessage;
import com.mtnz.njk.result.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 快递管理服务层
 * @Author: FBL
 * @CreateDate: 2019\8\26 0026 8:21
 * @Version: 1.0
 */
@Service
public class ExpressManagerService {

    @Autowired
    ExpressManagerMapper expressManagerMapper;

    @Autowired
    ExpressMapper expressMapper;

    /**
     * 快递管理展示列表
     *
     * @param key
     * @param beginTime
     * @param endTime
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    public ResponseResult listExpress(String key, String beginTime, String endTime, Integer pageNum, Integer pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize);
        // 查询快递列表
        List<ExpressEntity> expressEntities = expressManagerMapper.listExpress(key, beginTime, endTime);
        PageInfo<ExpressEntity> expressEntityPageInfo = new PageInfo<>(expressEntities);
        return new ResponseResult(ResponseMessage.OK, expressEntityPageInfo);
    }

    /**
     * 快递管理编辑或新增
     *
     * @param expressEntity
     * @return
     * @throws Exception
     */
    @Transactional
    public ResponseResult updateOrInsertExpress(ExpressEntity expressEntity) throws Exception {
        int i = 0;
        if (expressEntity.getExpressId() != null) {
            // 编辑
            expressEntity.setUpdateTime(new Date());
            i += expressMapper.updateByPrimaryKeySelective(expressEntity);
        } else {
            // 新增
            expressEntity.setCreateTime(new Date());
            expressEntity.setUpdateTime(new Date());
            i += expressMapper.insertSelective(expressEntity);
        }
        if (i > 0)
            return new ResponseResult(ResponseMessage.OK);
        return new ResponseResult(ResponseMessage.FAIL);
    }

    /**
     * 快递管理删除
     *
     * @param expressId
     * @return
     * @throws Exception
     */
    @Transactional
    public ResponseResult delExpress(Integer expressId) throws Exception {
        int i = expressMapper.deleteByPrimaryKey(expressId);
        if (i > 0)
            return new ResponseResult(ResponseMessage.OK);
        return new ResponseResult(ResponseMessage.FAIL);
    }

    /**
     * 快递管理表格导出
     *
     * @return
     * @throws Exception
     */
    public ResponseResult excelExpress(HttpServletResponse response) throws Exception {
        String[] header = {"省份", "姓名", "电话", "详细地址", "快递公司", "快递单号", "快递物品", "备注", "创建时间"};
        List<String[]> list = new ArrayList<>();
        List<ExpressEntity> lists = expressMapper.selectAll();
        for (int i = 0; i < lists.size(); i++) {
            String[] str = new String[9];
            str[0] = lists.get(i).getExpressCity();
            str[1] = lists.get(i).getExpressUserName();
            str[2] = lists.get(i).getExpressPhone();
            str[3] = lists.get(i).getExpressAddress();
            str[4] = lists.get(i).getExpressCompany();
            str[5] = lists.get(i).getExpressSendNum();
            str[6] = lists.get(i).getExpressSendGoods();
            str[7] = lists.get(i).getExpressInfo();
            String createTime = DateUtil.format(lists.get(i).getCreateTime(), "yyyy-MM-dd HH:mm:ss");
            str[8] = createTime;
            list.add(str);
        }
        ExcelUtils.createExcelMerge_simple(System.currentTimeMillis() + "_njk.xls", header, list, response);
        return new ResponseResult(ResponseMessage.OK);
    }
}
